package com.interview.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.interview.common.Result;
import com.interview.entity.Examee;
import com.interview.entity.GroupInfo;
import com.interview.service.DataDownLoadService;
import com.interview.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.*;

@Service
public class DataDownLoadServiceImpl implements DataDownLoadService {
    @Autowired
    private Environment environment;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public List<GroupInfo> getAvailableDownLoadGroupIds() {
        //配置的数据管理服务IP
        String dataCenterURL = environment.getProperty("DataCenterURL");
        String targetURL = dataCenterURL+"/dataDistribute/getAvailableDownLoadGroupInfo";

        ParameterizedTypeReference<List<GroupInfo>> responseType = new ParameterizedTypeReference<List<GroupInfo>>(){};
        // 请求头设置,x-www-form-urlencoded格式的数据
        ResponseEntity<List<GroupInfo>> response = restTemplate.exchange(targetURL,
                HttpMethod.POST,
                null,
                responseType);
        List<GroupInfo> groupInfoList = response.getBody();
        return groupInfoList;
    }

    @Override
    public List<GroupInfo> getGroupInfoByIdsFromDataCenter(String groupIds) throws IOException {
        String dataCenterURL = environment.getProperty("DataCenterURL");
        String targetURL = dataCenterURL+"/dataDistribute/groupInfo";

        ParameterizedTypeReference<Result> responseType = new ParameterizedTypeReference<Result>(){};
        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("ids",groupIds);

        // 组装请求体
        //发起请求(数据解析)
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<Result> response = restTemplate.exchange(targetURL,
                HttpMethod.POST,
                request,
                responseType);
        Result result = response.getBody();
        ArrayList<LinkedHashMap> data =(ArrayList<LinkedHashMap>) result.getData();
        ArrayList<GroupInfo> groupInfos1 = new ArrayList<>(data.size());
        for (LinkedHashMap datum : data) {
            String groupId =(String) datum.get("groupId");
            String account =(String) datum.get("account");
            GroupInfo groupInfo = new GroupInfo();
            groupInfo.setGroupId(groupId);
            groupInfo.setAccount(account);
            groupInfos1.add(groupInfo);
        }

        return groupInfos1;
    }

    @Override
    public List<Examee> getExameeInfoByIdsFromDataCenter(String groupIds) {
        String dataCenterURL = environment.getProperty("DataCenterURL");
        String targetURL = dataCenterURL+"/dataDistribute/exameeInfo";

        ParameterizedTypeReference<Result> responseType = new ParameterizedTypeReference<Result>(){};
        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("ids",groupIds);

        // 组装请求体
        //发起请求(数据解析)
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<Result> response = restTemplate.exchange(targetURL,
                HttpMethod.POST,
                request,
                responseType);
        Result result = response.getBody();
        ArrayList<LinkedHashMap> data =(ArrayList<LinkedHashMap>) result.getData();
        ArrayList<Examee> examees = new ArrayList<>(data.size());
        for (LinkedHashMap datum : data) {
            String idNumber =(String) datum.get("idNumber");
            String name =(String) datum.get("name");
            String groupId =(String) datum.get("groupId");

            Examee examee = new Examee();
            examee.setGroupId(groupId);
            examee.setIdNumber(idNumber);
            examee.setName(name);
            examees.add(examee);
        }

        return examees;
    }
}
